package io; /*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 /**
 * Project  : CookQQ
 * Package  : net.solosky.cookqq.client.myqq.net
 * File     : io.ByteWriter.java
 * Author   : solosky < solosky772@qq.com >
 * Created  : 2009-10-12
 * License  : Apache License 2.0 
 */

 import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 * <pre>
 *
 *  <code>
 * ChangeLog:
 *   2009-10-12	io.ByteWriter.java created.
 *  </code>
 * </pre>
 */
public class ByteWriter
{
	private ByteArrayOutputStream byteOutStream;
	private DataOutputStream out;
	
	
	public ByteWriter()
	{   byteOutStream = new ByteArrayOutputStream();
		out = new DataOutputStream(byteOutStream);
	}


	public int size()
    {
	    return byteOutStream.size();
    }


	public byte[] toByteArray()
    {
	    return byteOutStream.toByteArray();
    }


	public void write(byte[] b, int off, int len) throws IOException
    {
	    out.write(b, off, len);
    }


	public void write(byte[] b) throws IOException
    {
	    out.write(b);
    }


	public final void writeByte(int v) throws IOException
    {
	    out.writeByte(v);
    }


	public final void writeChar(int v) throws IOException
    {
	    out.writeChar(v);
    }


	public final void writeInt(int v) throws IOException
    {
	    out.writeInt(v);
    }


	public final void writeShort(int v) throws IOException
    {
        out.writeShort(v);
    }

    public final void writeShort2(int v) throws IOException
    {

        out.write(v & 0xFF);
        out.write((v >> 8) & 0xFF);

    }


    public final void writeUnicode(String s) throws IOException {
        writeByte((s.length() + 1) * 2);
       for(char c: s.toCharArray()) {
           writeShort2(c);
       }
       writeShort(0);
    }

    public final void writeToken(byte[] b) throws IOException {
       writeByte(b.length);
       write(b);
    }
	
	
	
}
